package com.sym.demo.pattern.startegy;


/**
 * @author suyongming
 * @date ：2021/3/7 16:41
 */
public class Sorter<T> {

    public static void sort(Comparable[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minPos = i;
            for(int j = i + 1; j < arr.length; j++) {
                minPos = arr[j].compareTo(arr[i]) == -1 ? j : minPos;
            }

            swap1(minPos,i,arr);
        }
    }


    private static void swap1(int minPos, int i, Comparable[] arr) {
        Comparable temp = arr[i];
        arr[i] = arr[minPos];
        arr[minPos] = temp;
    }

    public void sort(T[] arr, Comparator<T> comparator) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minPos = i;
            for(int j = i + 1; j < arr.length; j++) {
                minPos = comparator.compare(arr[j], arr[minPos]) == -1? j : minPos;
            }

            swap2(minPos,i,arr);
        }
    }


    private void swap2(int minPos, int i, T[] arr) {
        T temp = arr[i];
        arr[i] = arr[minPos];
        arr[minPos] = temp;
    }
}
